package com.lemon.smartsafety.ui.adapter.safe.inspection;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.entity.event.inspection.InspectionCheckItemViewDetailEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingCheckItemVo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.ui.adapter.safe.data.BuildingListAdapter;
import com.lemon.smartsafety.utils.common.DateUtils;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/29.
 */
public class BuildingInspectionCheckItemAdapter extends LemonRcvAdapter<CompanyBuildingCheckItemVo> {
    private RecyclerTClickListener<CompanyBuildingCheckItemVo> mListener;

    public BuildingInspectionCheckItemAdapter(@Nullable List<CompanyBuildingCheckItemVo> data, RecyclerTClickListener listener) {
        super(data);
        this.mListener = listener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new BuildingInspectionCheckItemItem();
    }

    public class BuildingInspectionCheckItemItem implements AdapterItem<CompanyBuildingCheckItemVo> {
        CardView     mainCardView;
        TextView     tvTitle;
        TextView     tvPatrol;
        TextView     tvCheckWeibao;
        TextView     tvCycle;
        TextView     tvFirePart;
        TextView     tvRemark;
        LinearLayout llLastCheck;
        LinearLayout llCheckStatus;
        LinearLayout llRemark;
        TextView     tvLastCheck;
        TextView     tvStatusTip;
        LinearLayout   rippleViewDetail;
        TextView     tvViewDetail;
        ImageView    imageArrow;
        ImageView ivStatusLight;


        private CompanyBuildingCheckItemVo mData;
        private int                        mPosition;

        @Override
        public int getLayoutResId() {
            return R.layout.item_inspection_check_item;
        }

        @Override
        public void bindViews(View root) {
            mainCardView = (CardView) root.findViewById(R.id.main_card_view);
            tvTitle = (TextView) root.findViewById(R.id.tv_title);
            tvPatrol = (TextView) root.findViewById(R.id.tv_patrol);
            tvCheckWeibao = (TextView) root.findViewById(R.id.tv_check_weibao);
            tvCycle = (TextView) root.findViewById(R.id.tv_cycle);
            tvFirePart = (TextView) root.findViewById(R.id.tv_fire_part);
            tvRemark = (TextView) root.findViewById(R.id.tv_remark);
            llLastCheck = (LinearLayout) root.findViewById(R.id.ll_last_check);
            llCheckStatus = (LinearLayout) root.findViewById(R.id.ll_check_status);
            llRemark = (LinearLayout) root.findViewById(R.id.ll_remark);
            tvLastCheck = (TextView) root.findViewById(R.id.tv_last_check);
            tvStatusTip = (TextView) root.findViewById(R.id.tv_status_tip);
            rippleViewDetail = (LinearLayout) root.findViewById(R.id.rippleViewDetail);
            tvViewDetail = (TextView) root.findViewById(R.id.tv_view_detail);
            //imageArrow = (ImageView) root.findViewById(R.id.imageArrow);
            ivStatusLight = (ImageView) root.findViewById(R.id.iv_status_light);
        }

        @Override
        public void setViews() {
            mainCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });

            rippleViewDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    InspectionCheckItemViewDetailEvent event = new InspectionCheckItemViewDetailEvent();
                    event.setTag(InspectionCheckItemViewDetailEvent.class.getName());
                    event.setData(mData);
                    AppService.getBus().post(event);
                }
            });


        /* 老的
         rippleViewDetail.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    InspectionCheckItemViewDetailEvent event = new InspectionCheckItemViewDetailEvent();
                    event.setTag(InspectionCheckItemViewDetailEvent.class.getName());
                    event.setData(mData);
                    AppService.getBus().post(event);
                }
            });*/
        }

        @Override
        public void handleData(CompanyBuildingCheckItemVo data, int position) {
            mData = data;
            mPosition = position;

            tvTitle.setText(StringUtils.nullValue(data.getObjectTitle()));
            if (StringUtils.getBoolean(data.getIsPatrol())) {
                tvPatrol.setText(DataInfo.getStatusString("是", DataInfo.ColorStatus.STATUS_BLUE));
            } else {
                tvPatrol.setText(DataInfo.getStatusString("否", DataInfo.ColorStatus.STATUS_GRAY));
            }


            if (StringUtils.getBoolean(data.getIsPatrol())) {
                tvCheckWeibao.setText(DataInfo.getStatusString("是", DataInfo.ColorStatus.STATUS_GREEN));
            } else {
                tvCheckWeibao.setText(DataInfo.getStatusString("否", DataInfo.ColorStatus.STATUS_GRAY));
            }
            tvCycle.setText(data.getCheckValidity() + data.getUnitName());

            tvRemark.setText(StringUtils.nullValue(data.getRemark(), "无"));
            if (StringUtils.nullValue(data.getRemark()).length() == 0) {
                llRemark.setVisibility(View.GONE);
            } else {
                llRemark.setVisibility(View.VISIBLE);
            }

            tvFirePart.setText(StringUtils.nullValue(data.getPlaceTypeName()));



            String sLastCheckUserName = StringUtils.nullValue(data.getLastCheckUserName());
            String sLastCheckDate = DateUtils.getFormatDateStringWithDateString(StringUtils.nullValue(data.getLastCheckDate()),DateUtils.dateFormatYMDHMS);
            tvLastCheck.setText(sLastCheckUserName + " " + sLastCheckDate);

            if (sLastCheckUserName.length() == 0) {
                llCheckStatus.setVisibility(View.GONE);
                tvLastCheck.setText(DataInfo.getStatusString("尚未检查", DataInfo.ColorStatus.STATUS_RED));
            } else {
                llCheckStatus.setVisibility(View.VISIBLE);
            }

            if (StringUtils.getBoolean(data.getIsDelay()) && sLastCheckUserName.length() != 0) {
                tvStatusTip.setVisibility(View.VISIBLE);
                tvStatusTip.setText(DataInfo.getStatusString("已超时，请重新检查", DataInfo.ColorStatus.STATUS_RED));
            }else{
                tvStatusTip.setVisibility(View.GONE);
            }

            String statusString = StringUtils.nullValue(data.getStatus(), "0");
            BuildingListAdapter.setStatusLight(statusString, ivStatusLight);

        }
    }



    public void setStatusLight(int position, ImageView ivStatusLight) {
        try {
            String statusString = StringUtils.nullValue(mDataList.get(position).getStatus(), "0");
            BuildingListAdapter.setStatusLight(statusString, ivStatusLight);
        } catch (Exception e) {

        }
    }

}
